﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wangxiaoyu_F.Models;

namespace wangxiaoyu_F.Controllers
{
    public class HomeController : Controller
    {
        nbaEntities db = new nbaEntities();
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            var list = db.t_teams.Select(p => new { tid = p.tid, tname = p.tname }).ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 表格显示
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPageList(int page, int limit, string pname, string pscore, string pscore1, string tid)
        {
            var dataList = db.t_players.Include("t_teams").AsEnumerable();
            var count = dataList.Count();

            if (!string.IsNullOrEmpty(pname))
            {
                dataList = dataList.Where(p => p.pname.Contains(pname));
            }
            if (!string.IsNullOrEmpty(pscore))
            {
                dataList = dataList.Where(p => p.pscore > Convert.ToInt32(pscore));
            }
            if (!string.IsNullOrEmpty(pscore1))
            {
                dataList = dataList.Where(p => p.pscore < Convert.ToInt32(pscore1));
            }
            if (!string.IsNullOrEmpty(tid))
            {
                dataList = dataList.Where(p => p.tid == Convert.ToInt32(tid));
            }

            var list = dataList.Select(p => new
            {
                pid = p.pid,
                pname = p.pname,
                ptime = p.ptime.ToString(),
                pscore = p.pscore,
                pscore1 = p.pscore,
                tid = p.tid,
                tname = p.t_teams.tname
            });

            return Json(new { count = count, data = list.OrderBy(p => p.pid).Skip((page - 1) * limit).Take(limit).ToList() });
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public ActionResult AddInfo(t_players players)
        {
            db.t_players.Add(players);
            int n = db.SaveChanges();
            if (n > 0)
            {
                return Json(new { code = 0, msg = "成功" });
            }
            else
            {
                return Json(new { code = -1, msg = "失败" });
            }
        }
    }
}